// pages/taskmanage/taskmanage.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userTasks: '',
    },
    addUserTask(event) {
        wx.navigateTo({
            url: `/pages/addtask/addtask`,
        });
    },
    deleteUserTask(event) {
        let user = wx.getStorageSync('user')
        const taskId = event.target.id;
        const data = this.data.userTasks.find(item => {
          return item._id === taskId && user.nickName === item.nick_name
        })
        console.log(data)
        const deleteTask = this.deleteTask;
        wx.showModal({
          title: '请确认',
          content: '删除 '+data.task_content,
          success: res => {
            if (res.confirm) {
              this.clearImg(data.task_image)
              deleteTask(data)
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
    },
     // 删除云端存储的图片
    clearImg(task_image) {
        wx.cloud.deleteFile({
          fileList: [task_image],
          success: res => {
            console.log(res.fileList)
          },
          fail: console.error
        })
    },
    deleteTask(data){
        wx.cloud.callFunction({
          name: 'babyCloudFuncs',
          data: {
            type: 'deleteTask',
            data: {
              taskId: data._id
            }
          }
        }).then(resp => {
          this.getUserAllTask()
        })
    }, 
    getUserAllTask () {
        let user = wx.getStorageSync('user')
        wx.cloud.callFunction({
            name: 'babyCloudFuncs',
            data: {
                type: 'showUserTask',
                data: {
                    nickName: user.nickName
                }
            }
        }).then((res) => {
            this.setData({
                userTasks: res.result.data
            })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getUserAllTask()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})